﻿//-----------------------------------------------------------------------
// <copyright file="Template.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System;
using System.Collections.Generic;
using FastNET.CleanAct.Common.Repository;
using FastNET.CleanAct.Common.Repository.Business;
using FastNET.CleanAct.Common.Repository.Interfaces.DataInterfaces.ManagementEngine;
using FastNET.CleanAct.Common.Repository.Interfaces.Factories;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.ManagementEngine;

#endregion

namespace FastNET.CleanAct.Client.ManagerEngineBusiness.DataObjects
{
    /// <summary>
    /// Business Class representing a Template for the client
    /// </summary>
    public class Template : IdentityBusinessBase<ITemplateData, ITemplateRepository>
    {
        #region AutoLoaded Property Fields



        #endregion

        #region Public CTor/DTor

        /// <summary>
        /// Initializes a new instance of the Template class
        /// </summary>
        public Template()
            : this(new RepositoryFactory())
        {

        }

        /// <summary>
        /// Initializes a new instance of the Template class
        /// </summary>
        /// <param name="ID">Identity of the data to retrieve</param>
        public Template(Identity ID)
            : this(new RepositoryFactory(), ID)
        {
        }

        #endregion

        #region Internal CTor/DTor

        /// <summary>
        /// Initializes a new instance of the Template class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="data">Data for backing the business object</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory or Data are null</exception>
        internal Template(IRepositoryFactory factory, ITemplateData data)
            : base(factory, data)
        {
        }

        /// <summary>
        /// Initializes a new instance of the Template class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        internal Template(IRepositoryFactory factory)
            : base(factory)
        {
        }

        /// <summary>
        /// Initializes a new instance of the Template class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="ID">Identity of the data to retrieve</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        internal Template(IRepositoryFactory factory, Identity ID)
            : base(factory, ID)
        {
        }

        #endregion

        #region Overrides of BusinessBase<ITemplateData,ITemplateRepository>

        /// <summary>
        /// Method to check whether or not the current object is valid
        /// </summary>
        /// <returns>Boolean based on the validity of the object</returns>
        public override bool IsValid()
        {
            return true;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the name for the template
        /// </summary>
        public string Name
        {
            get
            {
                return Data.Name;
            }

            set
            {
                Data.Name = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not the template is a distributed package or not
        /// </summary>
        public bool IsBuiltIn
        {
            get
            {
                return Data.IsBuiltIn;
            }

            set
            {
                Data.IsBuiltIn = value;
            }
        }

        /// <summary>
        /// Gets or sets the ID associated with the template's packaging
        /// </summary>
        public Identity? PackageID
        {
            get
            {
                return Data.PackageID;
            }

            set
            {
                Data.PackageID = value;
            }
        }

        #endregion

        #region Public Static Factory Methods

        /// <summary>
        /// Retrieves a list of templates
        /// </summary>
        /// <returns>IList of Template found or null if no items were found</returns>
        public static IList<Template> Retrieve()
        {
            return Retrieve(new RepositoryFactory());
        }

        /// <summary>
        /// Gets a list of templates by using their associated Package ID
        /// </summary>
        /// <param name="packageID">Identity of the package to retrieve by or NULL to retrieve unassocated packages</param>
        /// <returns>List of Template for all found packages</returns>
        public static IList<Template> RetrieveByPackageID(Identity? packageID)
        {
            return RetrieveByPackageID(packageID, new RepositoryFactory());
        }

        #endregion

        #region Internal Static Factory Methods

        #region Retrieve

        /// <summary>
        /// Retrieves a list of templates
        /// </summary>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <returns>IList of Template found or null if no items were found</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static IList<Template> Retrieve(IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get an account
            IList<ITemplateData> items = RetrieveRepository(factory).Retrieve();
            IList<Template> returnItems = new List<Template>();

            foreach (ITemplateData item in items)
                returnItems.Add(new Template(factory, item));

            return returnItems;
        }

        #endregion

        /// <summary>
        /// Gets a list of templates by using their associated Package ID
        /// </summary>
        /// <param name="packageID">Identity of the package to retrieve by or NULL to retrieve unassocated packages</param>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <returns>List of Template for all found packages</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static IList<Template> RetrieveByPackageID(Identity? packageID, IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get an account
            IList<ITemplateData> items = RetrieveRepository(factory).RetrieveByPackageID(packageID);
            IList<Template> returnItems = new List<Template>();

            foreach (ITemplateData item in items)
                returnItems.Add(new Template(factory, item));

            return returnItems;
        }

        #endregion
    }
}